﻿using System;
using System.Windows.Automation;

namespace AutoWinUI
{
    public class InternetOptions
    {

        #region General function
        public static void LaunchInternetOptions()
        {
            Desktop.LaunchProgram("inetcpl.cpl");
            System.Threading.Thread.Sleep(1000);
        }

        public static bool IsInternetOptionsLaunched()
        {
            bool answer = false;

            //find "Internet Options" existing on the desktop
            AutomationElement Desktop = AutomationElement.RootElement;
            AutomationElement InternetOptions = DetectInternetOptions();
            if (InternetOptions != null)
                answer = true;

            return answer;
        }

        public static void ClickCancelButton()
        {
            AutomationElement InternetOptions = DetectInternetOptions();
            if (InternetOptions != null)
            {
                AutomationElement CancelButton = null;
                AutoElement ele = new AutoElement();
                ele.AutomationID = "2";
                ele.ClassName = "Button";
                ele.ParentElement = InternetOptions;
                CancelButton = ele.FindFirst(SearchScope.Children);
                if (CancelButton != null)
                    PatternAction.InvokePatternInvokeAction(CancelButton);

                System.Threading.Thread.Sleep(500);
            }
        }
        
        public static void ClickOKButton()
        {
            AutomationElement InternetOptions = DetectInternetOptions();
            if (InternetOptions != null)
            {
                AutomationElement OKButton = null;
                AutoElement ele = new AutoElement();
                ele.AutomationID = "1";
                ele.ClassName = "Button";
                ele.ParentElement = InternetOptions;
                OKButton = ele.FindFirst(SearchScope.Children);
                if (OKButton != null)
                    PatternAction.InvokePatternInvokeAction(OKButton);

                System.Threading.Thread.Sleep(500);
            }
        }

        public static void ClickApplyButton()
        {
            AutomationElement InternetOptions = DetectInternetOptions();
            if (InternetOptions != null)
            {
                AutomationElement ApplyButton = null;
                AutoElement ele = new AutoElement();
                ele.AutomationID = "12321";
                ele.ClassName = "Button";
                ele.ParentElement = InternetOptions;
                ApplyButton = ele.FindFirst(SearchScope.Children);
                if (ApplyButton != null)
                    PatternAction.InvokePatternInvokeAction(ApplyButton);

                System.Threading.Thread.Sleep(500);
            }
        }

        public static void ClickUseBlank()
        {
            AutomationElement InternetOptions = DetectInternetOptions();
            if (InternetOptions != null)
            {
                AutomationElement UseBlankButton = null;
                AutoElement ele = new AutoElement();
                ele.AutomationID = "404";
                ele.ClassName = "Button";
                ele.ParentElement = InternetOptions;
                UseBlankButton = ele.FindFirst(SearchScope.Descendants);
                if (UseBlankButton != null)
                    PatternAction.InvokePatternInvokeAction(UseBlankButton);

                System.Threading.Thread.Sleep(500);
            }
        }

        public static void ClickTabSetting()
        {
            AutomationElement InternetOptions = DetectInternetOptions();
            if (InternetOptions != null)
            {
                AutomationElement TabBrowsing = null;
                AutoElement ele = new AutoElement();
                ele.AutomationID = "5505";
                ele.ClassName = "Button";
                ele.ParentElement = InternetOptions;
                TabBrowsing = ele.FindFirst(SearchScope.Descendants);  
                if (TabBrowsing != null)
                    PatternAction.InvokePatternInvokeAction(TabBrowsing);

                System.Threading.Thread.Sleep(500);
            }
            
        }

        #endregion

        #region Tab Browsing function

        public static void ClickCancelonTabBrowsing()
        {
            AutomationElement Tab = DetectTabBrowsingWindow();
            if (Tab != null)
            {
                PropertyCondition AutomationID = new PropertyCondition(AutomationElement.AutomationIdProperty, "2");
                PropertyCondition ClassName = new PropertyCondition(AutomationElement.ClassNameProperty, "Button");
                AndCondition and1 = new AndCondition(AutomationID, ClassName);
                AutomationElement cancel = Tab.FindFirst(TreeScope.Children, and1);
                if (cancel != null)
                    PatternAction.InvokePatternInvokeAction(cancel);

                System.Threading.Thread.Sleep(500);
            }
        }

        #endregion


        private static AutomationElement DetectInternetOptions()
        {
            AutomationElement Desktop = AutomationElement.RootElement;
            AutomationElement InternetOptions = null;
            AutoElement ele = new AutoElement();
            ele.AutomationID = "";
            ele.ClassName = "#32770";
            ele.ParentElement = Desktop;
            InternetOptions = ele.FindFirst(SearchScope.Children);
            return InternetOptions;
        }

        private static AutomationElement DetectTabBrowsingWindow()
        {
            AutomationElement InternetOptions = DetectInternetOptions();
            if (InternetOptions == null)
                return null;
            else
            {
                AutoElement ele = new AutoElement();
                ele.AutomationID = "";
                ele.ClassName = "#32770";
                ele.ParentElement = InternetOptions;
                return ele.FindFirst(SearchScope.Children);
            }
        }



    }

}
